﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class ActionPanel : HUDPanel
{
	public enum ActionID
	{
		WalkTowards		= 0,
		AttackZombie	= 1,
		PickObjectUp	= 2,
		PerformAction	= 3,
		SkipAction		= 4,
		COUNT,
	}
	
	public GameObject usualButtonsGroup;
	public GameObject selectCurrentGroup;
	public GameObject zombiesPlayingGroup;

	public Text characterName;
	public Text actionsLeft;
	public Animator[] animators;
	private ActionID previousID = ActionID.COUNT;
	private bool[] states;

	private void Awake()
	{
		states = new bool[animators.Length];
	}

	// Called by HUDScreen when a character has been modified or selected
	public override void UpdatePanel(Character _character)
	{
		DeactivateAllButtons();
		actionsLeft.text = "Actions Left : " + _character.currentActions;

		characterName.text = InitiativeManager.Instance.current.GetInfo().itemName;
		usualButtonsGroup.SetActive(false);
		selectCurrentGroup.SetActive(false);
		zombiesPlayingGroup.SetActive(false);
		if (_character == InitiativeManager.Instance.current)
		{
			usualButtonsGroup.SetActive(true);
		}
		else
		{
			if (InitiativeManager.Instance.current is Player)
			{
				selectCurrentGroup.SetActive(true);
			}
			else
			{
				zombiesPlayingGroup.SetActive(true);
			}
		}
	}
	
	public void DeactivateAllButtons()
	{
		for (int i = 0; i < states.Length; i++)
		{
			DeactivateButton((ActionID)i);
		}
	}
	
	public void OnClick_WalkTowards(Animator _anim)
	{
		ActionButton(ActionID.WalkTowards, _anim);
	}
	
	public void OnClick_AttackZombie(Animator _anim)
	{
		ActionButton(ActionID.AttackZombie, _anim);
	}
	
	public void OnClick_PickObjectUp(Animator _anim)
	{
		ActionButton(ActionID.PickObjectUp, _anim);
	}
	
	public void OnClick_PerformAction(Animator _anim)
	{
		ActionButton(ActionID.PerformAction, _anim);
	}
	
	public void OnClick_SkipAction()
	{
		ActionButton(ActionID.SkipAction, null);
	}

	public void OnClick_CurrentCharacter()
	{
		HUDScreen.Instance.SetupCharacter(InitiativeManager.Instance.current);
	}

	private void DeactivateButton(ActionID _id)
	{
		if (_id != ActionID.COUNT && _id != ActionID.SkipAction)
		{
			states[(int)_id] = false;
			animators[(int)_id].SetBool("Pressed", false);
		}
	}

	private Tile centerTile = null;
	private void ActionButton(ActionID _id, Animator _anim)
	{
		if (_id != ActionID.SkipAction)
		{
			states[(int)_id] = !states[(int)_id];
			animators[(int)_id].SetBool("Pressed", states[(int)_id]);
		}

		if (previousID != _id)
		{
			DeactivateButton(previousID);
			previousID = _id;
		}

		// Erase previous tiles and interactives
		if (centerTile)
		{
			AreaCreator.RemoveArea();
			FloorManager.Instance.DeactivateInteractives();
		}

		centerTile = InitiativeManager.Instance.current.currentTile;
		switch (_id)
		{
		case ActionID.AttackZombie:
			if (states[(int)_id])
			{
				AreaCreator.ActivateArea(centerTile, InitiativeManager.Instance.current, Tile.State.Attack, ScanFinished);
			}
			break;
			
		case ActionID.WalkTowards:
			if (states[(int)_id])
			{
				AreaCreator.ActivateArea(centerTile, InitiativeManager.Instance.current, Tile.State.Walk, ScanFinished);
			}
			break;
			
		case ActionID.PickObjectUp:
			if (states[(int)_id])
			{
				AreaCreator.ActivateArea(centerTile, InitiativeManager.Instance.current, Tile.State.PickUp, ScanFinished);
			}
			break;
			
		case ActionID.PerformAction:
			if (states[(int)_id])
			{
				AreaCreator.ActivateArea(centerTile, InitiativeManager.Instance.current, Tile.State.Action, ScanFinished);
			} 
			break;
			
		case ActionID.SkipAction:
			InitiativeManager.Instance.current.FinishAction();
			break;
		}
	}

	private void ScanFinished(int _interactivesFound)
	{
		if (_interactivesFound == 0)
		{
			if (previousID != ActionID.WalkTowards)
			{
				DeactivateButton(previousID);
			}
		}
	}
}
